{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 3.8 \u5206\u6570\u8fd0\u7b97\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u8fdb\u5165\u65f6\u95f4\u673a\u5668\uff0c\u7a81\u7136\u53d1\u73b0\u4f60\u6b63\u5728\u505a\u5c0f\u5b66\u5bb6\u5ead\u4f5c\u4e1a\uff0c\u5e76\u6d89\u53ca\u5230\u5206\u6570\u8ba1\u7b97\u95ee\u9898\u3002\n\u6216\u8005\u4f60\u53ef\u80fd\u9700\u8981\u5199\u4ee3\u7801\u53bb\u8ba1\u7b97\u5728\u4f60\u7684\u6728\u5de5\u5de5\u5382\u4e2d\u7684\u6d4b\u91cf\u503c\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "fractions \u6a21\u5757\u53ef\u4ee5\u88ab\u7528\u6765\u6267\u884c\u5305\u542b\u5206\u6570\u7684\u6570\u5b66\u8fd0\u7b97\u3002\u6bd4\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "from fractions import Fraction\na = Fraction(5, 4)\nb = Fraction(7, 16)\nprint(a + b)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "print(a * b)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Getting numerator/denominator\nc = a * b\nc.numerator"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "c.denominator"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Converting to a float\nfloat(c)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Limiting the denominator of a value\nprint(c.limit_denominator(8))"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Converting a float to a fraction\nx = 3.75\ny = Fraction(*x.as_integer_ratio())\ny"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5728\u5927\u591a\u6570\u7a0b\u5e8f\u4e2d\u4e00\u822c\u4e0d\u4f1a\u51fa\u73b0\u5206\u6570\u7684\u8ba1\u7b97\u95ee\u9898\uff0c\u4f46\u662f\u6709\u65f6\u5019\u8fd8\u662f\u9700\u8981\u7528\u5230\u7684\u3002\n\u6bd4\u5982\uff0c\u5728\u4e00\u4e2a\u5141\u8bb8\u63a5\u53d7\u5206\u6570\u5f62\u5f0f\u7684\u6d4b\u8bd5\u5355\u4f4d\u5e76\u4ee5\u5206\u6570\u5f62\u5f0f\u6267\u884c\u8fd0\u7b97\u7684\u7a0b\u5e8f\u4e2d\uff0c\n\u76f4\u63a5\u4f7f\u7528\u5206\u6570\u53ef\u4ee5\u51cf\u5c11\u624b\u52a8\u8f6c\u6362\u4e3a\u5c0f\u6570\u6216\u6d6e\u70b9\u6570\u7684\u5de5\u4f5c\u3002"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.7.1"
    },
    "toc": {
      "base_numbering": 1,
      "nav_menu": {},
      "number_sections": true,
      "sideBar": true,
      "skip_h1_title": true,
      "title_cell": "Table of Contents",
      "title_sidebar": "Contents",
      "toc_cell": false,
      "toc_position": {},
      "toc_section_display": true,
      "toc_window_display": true
    }
  },
  "nbformat": 4,
  "nbformat_minor": 2
}